#include <iostream>
#include <string>
#include <sstream>
#include <jsoncpp/json/json.h>
#include <memory>

int main()
{
    //反序列化
    std::string json_string = "{\"name\":\"张三\", \"age\":30, \"city\":\"北京\"}";

    Json::Value root;
    Json::Reader reader;
    reader.parse(json_string, root);

    std::string name = root["name"].asString();
    int age = root["age"].asInt();
    std::string city = root["city"].asString();

    std::cout << name << std::endl;
    std::cout << age << std::endl;
    std::cout << city << std::endl;

    //序列化
    // Json::Value root;
    // root["name"] = "mike";
    // root["sex"] = "man";
    // root["age"] = 18; 


    // Json::StreamWriterBuilder sbuilder;
    // std::unique_ptr<Json::StreamWriter> writer(sbuilder.newStreamWriter());
    // std::stringstream ss;
    // writer->write(root, &ss);
    // std::string s = ss.str();
    // std::cout << s << std::endl;

    //带空格的结构化子串
    //{
    //     "age" : 18,
    //     "name" : "mike",
    //     "sex" : "man"
    // }
    // Json::StyledWriter writer;
    // std::string s = writer.write(root);
    // std::cout << s << std::endl;

    //不带空格的结构化子串
    //{"age":18,"name":"mike","sex":"man"}
    //Json::FastWriter writer;
    // std::string s = writer.write(root);
    // std::cout << s << std::endl;

    // Json::Value sub;
    // sub["tel"] = "123";
    // sub["age"] = 18;
    // std::string s = sub.toStyledString();
    // std::cout << s << std::endl;

    return 0;
}